Модуль:Util
Functions --- -- UTF-8 Reference: -- 0xxxxxxx - 1 byte UTF-8 codepoint (ASCII character) -- 110yyyxx - First byte of a 2 byte UTF-8 codepoint -- 1110yyyy - First byte of a 3 byte UTF-8 codepoint -- 11110zzz - First byte of a 4 byte UTF-8 codepoint -- 10xxxxxx - Inner byte of a multi-byte UTF-8 codepoint local function chsize(char) if not char then return 0 elseif char > 240 then return 4 elseif char > 225 then return 3 elseif char > 192 then return 2 else return 1 end end -- This function can return a substring of a UTF-8 string, properly handling -- UTF-8 codepoints. Rather than taking a start index and optionally an end -- index, it takes the string, the starting character, and the number of -- characters to select from the string. local function utf8sub(str, startChar, numChars) local startIndex = 1 while startChar > 1 do local char = string.byte(str, startIndex) startIndex = startIndex + chsize(char) startChar = startChar - 1 end local currentIndex = startIndex while numChars > 0 and currentIndex <= #str do local char = string.byte(str, currentIndex) currentIndex = currentIndex + chsize(char) numChars = numChars -1 end return str:sub(startIndex, currentIndex - 1) end --- function explode(div,str) -- credit: http://richard.warburton.it if (div '') then return false end local pos,arr = 0,{} -- for each divider found for st,sp in function() return string.find(str,div,pos,true) end do table.insert(arr,string.sub(str,pos,st-1)) -- Attach chars left of current divider pos = sp + 1 -- Jump past current divider end table.insert(arr,string.sub(str,pos)) -- Attach chars right of last divider return arr end function tablelength(T) local count = 0 for _ in pairs(T) do count = count + 1 end return count end --- Body --- local p = {} function p.dialog(frame) data = ' ' frame = (frame:getParent() or frame) if (frame.args1:gsub("%s*",'') ~= "") then data = data .. ' ' .. (frame.args1 or "| Заголовок Диалога, пжлст") :match( "^%s*(.-)%s*$" ) .. ' ' end for i = 2, tablelength(frame.args), 2 do data = data .. ' ' .. (frame.argsi or "| Говорящий | Фраза, пжлст") :match( "^%s*(.-)%s*$" ) .. ': ' .. (frame.argsi+1 or ""):match( "^%s*(.-)%s*$" ) .. ' ' end return data .. ' ' end function p.links(frame) qwert = mw.text.split((frame.args1 or "") :gsub("%[%""):gsub("%%]", ""), ",") -- explode(",", (frame.args1 or ""):gsub("%[%""):gsub("%%]", "")) if qwert ~= "" then data = '[.. qwert[1 :match( "^%s*(.-)%s*$" ) .. ']]' for i = 2, tablelength(qwert) do data = data .. ', [.. qwert[i :match( "^%s*(.-)%s*$" ) .. ']]' end data = data:gsub("%[%[%[",':gsub("%%]%]",']') end return data end function p.gender(frame) qwert = (frame.args1 or "") qwert = utf8sub(qwert,1,1):lower() if (qwert "f" or qwert "ж" or qwert "Ж") then data = '♀' elseif (qwert "m" or qwert "м" or qwert "М") then data = '♂' else data = qwert end return data end function p.dragonscript(frame) frame = (frame:getParent() or frame) tag = {} tag1 = '<' .. (frame.args3 or "span") .. ' class="Dragonic-font" style="font-size: ' .. (frame.args2 or "20px") .. '; line-height: 1.1; word-spacing: .5em;">' tag2 = '' string = (frame.args1 or "") :match( "^%s*(.-)%s*$" ) -- Trim space ?? :lower() -- text lowercase! :gsub("\n" ,' ') -- prevent paragraph division -- dict :gsub('aa','1'):gsub('ah','4') :gsub('ei','2'):gsub('ey','9') :gsub('ii','3'):gsub('ir','7') :gsub('oo','8'):gsub('uu','5') :gsub('ur','6'):gsub('_' ,' ') return tag1 .. string .. tag2 end return p